package com.itheima01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;

/**
 * @author Xiao
 * @date 2021/7/14
 */
public class CollectionDemo02 {
    /**
     * Collection集合常用方法
     */
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();   //创建对象
        c.add("hello");
        c.add("world");
        c.add("itheima");
        c.add("itheima");
        System.out.println(c + "\t" + c.size());    //[hello, world, itheima, itheima]	4
        c.remove("itheima");    //只能删除第一个"itheima"
        System.out.println(c);      //[hello, world, itheima]
//        c.removeIf((String s) -> {return s.length() == 5;});
        c.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                if (s.length() == 7) {
                    return true;
                }
                return false;
            }
        });
//        c.removeIf(s -> s.length() == 5);   //此方法参数为Lambda表达式：将字符串长度为5的元素删除
        System.out.println(c);      //[itheima]
        boolean b1 = c.contains("itheima");     //true
        boolean b2 = c.contains("1");   //false
        boolean b3 = c.isEmpty();   //false
        System.out.println(b1 + "\t" + b2 + "\t" + b3);
        c.clear();
        System.out.println(c);      //[]
        boolean b4 = c.isEmpty();
        System.out.println(b4);     //true


    }
}
